import axios from 'axios';

const API_URL = 'https://localhost:5005/api/userApp';

/**
 * 获取用户APP列表
 * @param {Object} params - 查询参数
 * @returns {Promise} 标准化响应对象
 */
export const getUserApps = async (params = {}) => {
  try {
    const defaultParams = {
      page: 1,
      pageSize: 10,
      sortBy: 'name',
      sortDirection: 'asc',
      search: ''
    };
    
    const requestParams = { ...defaultParams, ...params };
    
    const response = await axios.get(API_URL, { 
      params: requestParams,
      headers: {
        'Accept': 'application/json'
      }
    });

    const responseData = response.data || {};
    
    return {
      success: responseData.Success,
      data: {
        items: responseData.Data?.Items || [],
        total: responseData.Data?.Total || 0
      },
      message: responseData.Message || ''
    };
    
  } catch (error) {
    console.error('获取用户APP列表失败:', error);
    const errorResponse = error.response?.data || {};
    return {
      success: false,
      message: errorResponse.Message || '获取用户APP列表失败',
      error: errorResponse.Error || error.message,
      data: {
        items: [],
        total: 0
      }
    };
  }
};

/**
 * 删除用户APP
 * @param {number|string} id - 要删除的条目ID
 * @returns {Promise} 标准化响应对象
 */
export const deleteUserApp = async (id) => {
  try {
    const response = await axios.delete(`${API_URL}/${id}`, {
      headers: {
        'Accept': 'application/json'
      }
    });

    const responseData = response.data || {};
    
    return {
      success: responseData.Success,
      message: responseData.Message || '删除成功',
      data: responseData.Data
    };
    
  } catch (error) {
    console.error('删除用户APP失败:', error);
    const errorResponse = error.response?.data || {};
    return {
      success: false,
      message: errorResponse.Message || '删除用户APP失败',
      error: errorResponse.Error || error.message
    };
  }
};

/**
 * 新增用户APP
 * @param {Object} data - 新增数据
 * @returns {Promise} 标准化响应对象
 */
export const addUserApp = async (data) => {
  try {
    const response = await axios.post(API_URL, data, {
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      }
    });

    const responseData = response.data || {};
    
    return {
      success: responseData.Success,
      message: responseData.Message || '新增成功',
      data: responseData.Data
    };
    
  } catch (error) {
    console.error('新增用户APP失败:', error);
    const errorResponse = error.response?.data || {};
    return {
      success: false,
      message: errorResponse.Message || '新增用户APP失败',
      error: errorResponse.Error || error.message
    };
  }
};

/**
 * 更新用户APP
 * @param {number|string} id - 要更新的条目ID
 * @param {Object} data - 更新数据
 * @returns {Promise} 标准化响应对象
 */
export const updateUserApp = async (id, data) => {
  try {
    const response = await axios.put(`${API_URL}/${id}`, data, {
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      }
    });

    const responseData = response.data || {};
    
    return {
      success: responseData.Success,
      message: responseData.Message || '更新成功',
      data: responseData.Data
    };
    
  } catch (error) {
    console.error('更新用户APP失败:', error);
    const errorResponse = error.response?.data || {};
    return {
      success: false,
      message: errorResponse.Message || '更新用户APP失败',
      error: errorResponse.Error || error.message
    };
  }
};